<!DOCTYPE HTML>
<html>

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="maximum-scale=1.0, minimum-scale=1.0, user-scalable=0, initial-scale=1.0, width=device-width" />
    <meta name="format-detection" content="telephone=no, email=no, date=no, address=no">
    <title>Hello APP</title>
    <link rel="stylesheet" type="text/css" href="../css/aui-flex.css" />
    <link rel="stylesheet" type="text/css" href="../css/aui.css" />
    <link rel="stylesheet" type="text/css" href="../css/common.css" />
    <style>
        /*弹窗*/
        body {
            background: transparent;
        }
        .innerBox{
          padding: 0 1rem;
        }
        .popBox {
          width: 100%;
            /*right: 0.5rem;*/
            bottom: 0;
            /*border-radius: 0.2rem;*/
        }
        .pay-list li{
          height: 2.5rem;
          /*padding: 0 0.75rem;*/
          font-size:0.7rem;
          color:#444444;
        }
        .pay-list li .imgBox{
          border-radius: 50%;
          margin-right: 0.3rem;
        }
        .pay-list li img{
          height: 1rem;
        }
        .comBtn{
          height: 2.5rem;
          color:#fff;
          line-height: 2.5rem;
          margin-top: 1.25rem;
        }
        .headBox{
          padding: 1rem 0;
          border-bottom: 1px solid #e7e7e7;
        }
        .headBox .tit{
          text-align: center;
        }

    </style>
</head>
<body>
    <div class="popOverlay" tapmode onClick="api.closeFrame();"></div>
    <div class="popBox">
      <div class="innerBox">
        <div class="headBox aui-flex-col aui-flex-between aui-flex-middle">
          <div></div>
          <div class="tit flex-auto aui-padded-l-10">订单支付</div>
          <div class="closeP" tapmode onclick="closeMask()"></div>
        </div>
        <ul class="pay-list">
          <li class="aui-flex-col aui-flex-middle aui-flex-between aui-border-b" tapmode onclick="checkPay(this)">
            <div class="imgBox"><img src="../image/common/pay1.png" alt=""></div>
            <p class="flex-auto">微信支付</p>
            <div class="checkbox checkItem checked" data-type="1"></div>
          </li>
          <li class="aui-flex-col aui-flex-middle aui-border-b" tapmode onclick="checkPay(this)">
            <div class="imgBox"><img src="../image/common/pay2.png" alt=""></div>
            <p class="flex-auto">支付宝支付</p>
            <div class="checkbox checkItem" data-type="2"></div>
          </li>
        </ul>
      </div>
      <div class="comBtn bg-e7" tapmode onclick="save()">立即支付</div>
    </div>
</body>
<script type="text/javascript" src="../script/import.js"></script>
<script type="text/javascript" src="../script/common.js"></script>
<script type="text/javascript">
var isSave = false;
var orderId;
var totalPrice;
var orderSn;
var postData = {};
apiready = function() {
      orderId = api.pageParam.orderId;
      orderSn = api.pageParam.orderSn
      totalPrice = api.pageParam.price;
      api.parseTapmode();
  };
    function checkPay(el){
    	var _this = $$(el).find('.checkbox');
    	var payType = _this.data('type');
    	// if(payType=='2'){
    	// 	toast('该功能正在开发中，请选择微信支付');
    	// 	return;
    	// }
    	$$('.pay-list .checkbox').removeClass('checked');
    	_this.addClass('checked');
    }

    function save(){
    	if(isSave){
    		return;
    	}
    	var payment = $$('.pay-list .checkbox.checked').data('type');
      if(payment == 1){
        pay_wx();
      }
      if(payment == 2){
        orderSn = api.pageParam.orderSn
        api.ajax({
          url: BASE_URL_2 + 'App/Mall/aliPay',
          method: 'post',
          data: {
            values: {
              tradeNo: orderSn,
              type: 1
            }
          }
        },function(ret, err){
          if (ret) {
            if(ret.code == 1){
                pay_ali(ret);
            }else{
              toast(ret.msg);
            }

          }
        });
      }
    }
    function pay_wx(){
      orderId = api.pageParam.orderId;
      orderSn = api.pageParam.orderSn;
      totalPrice = api.pageParam.price;
    	var wxPay = api.require('wxPay');
    	wxPay.config({
    		apiKey: '',
    		mchId: '1516763411',
    		partnerKey: '5ul24roehvlsoizd6ye94sh2ccl8s3v8',
    		notifyUrl: 'http://taoke.maiyatang.info/App/Notify/wxMallPaySuccessNotifyOne'
    	}, function(result, err) {
    		if (result.status) {
    			wxPay.pay({
    				description: '商品购买',
    				totalFee: totalPrice*100,
            // totalFee: 1,
    				tradeNo: orderSn,
    				detail: '商品购买',
    				feeType: 'CNY'
    			}, function(ret, err) {
    				if (ret.status) {
    				toast('支付成功');
            api.closeFrame();
            api.closeWin({
                name: 'order_detail_win'
            });
            api.sendEvent({
                name: 'homeEvent'
            });
            openPay();
    				} else {
    					api.alert({
    						title: '支付消息',
    						msg: err.msg||'支付失败',
    					}, function(ret, err) {
    					});
    				}
    			});
    		} else {
    			toast('支付失败');
    		}
    	});
    }
    // 支付宝支付
    function pay_ali(ret){
    	var aliPayPlus = api.require('aliPayPlus');
    	aliPayPlus.payOrder({
    		orderInfo: ret.response
    	}, function(alipayRet, err) {
    		if(alipayRet){
    			switch (alipayRet.code) {
    				case '9000':
    					openPay();
    					break;
    				case '4000':
    					toast('支付失败');
    					break;
    				case '6001':
    					toast('取消支付');
    					break;
    				default:
    					toast('支付失败');
    				break;
    			}

    		}else{
    			toast('支付失败');
    		}

    	});
    }
    function openPay(){
      api.sendEvent({
    		name: 'paySuccessEvent'
    	});
      totalPrice = api.pageParam.price;
      api.openFrame({
        name:'mask_pay_success',
        url:'widget://html/mask_pay_success.html',
        rect: {
          x: 0,
          y: 0,
          w: 'auto',
          h: 'auto'
        },
        animation: {
          type: 'fade',
          duration:300
        },
        pageParam: {
    			price: totalPrice
    		}
      })
    }
</script>
</html>
